package com.xhb.campusservices.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xhb.campusservices.common.lang.Result;
import com.xhb.campusservices.enums.FeedbackState;
import com.xhb.campusservices.pojo.Feedback;
import com.xhb.campusservices.pojo.Feedbacktype;
import com.xhb.campusservices.pojo.feedbackDto.FeedbackList;
import com.xhb.campusservices.pojo.feedbackDto.ReplyFB;
import com.xhb.campusservices.service.FeedbackListService;
import com.xhb.campusservices.service.FeedbackService;
import com.xhb.campusservices.service.FeedbacktypeService;
import com.xhb.campusservices.util.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户反馈
 */
@Slf4j
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;

    @Autowired
    FeedbackListService feedbackListService;

    @Autowired
    FeedbacktypeService feedbacktypeService;

    @Autowired
    JWTUtils jwtUtils;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 反馈类型
     */
    @GetMapping("/feedbackType")
    public Result feedbackType() {
        List<Feedbacktype> list = feedbacktypeService.list();
        return Result.succ(list);
    }

    /**
     * 普通用户查看自己的反馈信息表
     * @param userId
     * @param request
     * @return
     */
    @GetMapping("/fbOrdinary/{userId}")
    public Result fbOrdinary(@PathVariable(name = "userId") Long userId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        //查询账号是否一致
        if (String.valueOf(userId).equals(jwtUtils.getUserId(token))) {
            List<FeedbackList> fbLists = feedbackListService.feedbackListOrdinary(userId);
            if (fbLists.size() == 0) {
                return Result.succ(200, "主人，您还没有反馈的信息哦~", null);
            }
            return Result.succ(fbLists);
        }
        return Result.fail(403, "没有查看权限");
    }

    /**
     * 新增反馈
     * @param feedback
     * @param request
     * @return
     */
    @PostMapping("/addFeedback")
    public Result addFeedback(@Validated @RequestBody Feedback feedback, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        //查询账号是否一致
        if (String.valueOf(feedback.getUserid()).equals(jwtUtils.getUserId(token))) {
            try {
                //设置反馈时间为当前时间
                feedback.setReleasetime(sdf.parse(sdf.format(new Date())));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (feedbackService.save(feedback)) {
                return Result.succ(200, "反馈成功", feedback);
            } else {
                return Result.fail("反馈失败");
            }
        }else {
            return Result.fail(403, "没有反馈权限!");
        }
    }
}
